package com.camelot.boot.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DeepSeekResumeExtractorNet {

    private static final String DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"; // 假设的 API 地址
    private static final String API_KEY = "sk-cc3f800cb24b4470871afd2923cdd571"; // 替换为你的 API Key

    public static String extractResumeInfo(String resumeText) throws IOException {
        String prompt = "请从以下简历中提取结构化信息（JSON 格式），包括：姓名、电话、邮箱、教育经历、工作经历:\n" + resumeText;

        String requestBody = "{"
                + "\"model\": \"deepseek-reasoner\","//deepseek-reasoner
                + "\"messages\": [{\"role\": \"user\", \"content\": \"" + prompt + "\"}],"
                + "\"temperature\": 0.3"
                + "}";

        URL url = new URL(DEEPSEEK_API_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Authorization", "Bearer " + API_KEY);
        connection.setDoOutput(true);

        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = requestBody.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
                StringBuilder response = new StringBuilder();
                String responseLine;
                while ((responseLine = br.readLine()) != null) {
                    response.append(responseLine.trim());
                }
                return response.toString();
            }
        } else {
            throw new IOException("API 请求失败，HTTP 状态码: " + responseCode);
        }
    }

    public static void main(String[] args) {
        String resumeText = "张三\n电话：13800138000\n邮箱：zhangsan@example.com\n"
                + "教育背景：\n- 北京大学 计算机科学 本科 2015-2019\n"
                + "工作经历：\n- 腾讯 软件开发工程师 2020-2023";

        try {
            String extractedInfo = extractResumeInfo(resumeText);
            System.out.println("提取结果：\n" + extractedInfo);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


